package com.hmall.api.fallback;

import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import com.hmall.common.exception.BizIllegalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;

import java.util.Collection;
import java.util.List;

@Slf4j
public class ItemClientFallback implements FallbackFactory<ItemClient> {



    @Override
    public ItemClient create(Throwable cause) {
        return  new ItemClient() {

            @Override
            public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
                log.error("调用商品id集和查询商品列表失败,具体参数为：{}",ids,cause);
                return List.of();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                //这个是扣减商品库存的接口:与下单的业务有关联，如果出错了需要将异常抛出来
                log.error("调用商品id集和查询商品列表失败,具体参数为：{}",items,cause);
                throw new BizIllegalException("扣减库存失败");
            }

            @Override
            public ItemDTO queryItemById(Long id) {
                log.error("调用queryItemById方法出现异常,具体参数为：{}",id,cause);
                return null;
            }
        };
    }
}
